package com.lizx.framework.web.smart.framework.core.utils;

import org.apache.log4j.Logger;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * 配置文件解析类
 * @Author: lizx
 * @Description:
 * @Date: Create in 2019/1/17 2:22 PM
 */
public class SmartPropUtils {
    private static final Logger LOGGER = Logger.getLogger(SmartPropUtils.class);

    /**
     * 加载属性文件
     *
     * @param fileName
     * @return
     */
    public static Properties loadProps(String fileName) {
        Properties properties = null;
        InputStream inputStream = null;
        try {
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (inputStream == null) {
                throw new FileNotFoundException(fileName + "is not find");
            }

            properties = new Properties();
            properties.load(inputStream);
        } catch (IOException e) {
            LOGGER.error("properies load failure" + e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.error("properies load failure" + e.getMessage());
            }
        }
        return properties;
    }

    /**
     * 获取字符串实行(默认值为空字符串)
     * @param properties
     * @param key
     * @return
     */
    public static String getString(Properties properties, String key) {
        return getString(properties, key, "");
    }

    /**
     * 获取字符串属性(可指定默认值)
     *
     * @param properties
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getString(Properties properties, String key, String defaultValue) {
        String value = defaultValue;
        if (properties.containsKey(key)) {
            value = properties.getProperty(key);
        }
        return value;
    }

    /**
     * 获取数值型属性(默认值为0)
     * @param properties
     * @param key
     * @return
     */
    public static int getInt(Properties properties, String key){
        return getInt(properties, key, 0);
    }

    /**
     * 获取数值型属性(可指定默认值)
     * @param properties
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(Properties properties,String key,int defaultValue){
        int value = defaultValue;
        if (properties.containsKey(key)){
            value = Integer.valueOf(properties.getProperty(key));
        }
        return value;
    }

    /**
     * 获取数值型属性(默认值为0)
     * @param properties
     * @param key
     * @return
     */
    public static Boolean getBoolean(Properties properties, String key){
        return getBoolean(properties, key, false);
    }

    /**
     * 获取数值型属性(可指定默认值)
     * @param properties
     * @param key
     * @param defaultValue
     * @return
     */
    public static Boolean getBoolean(Properties properties,String key,boolean defaultValue){
        boolean value = defaultValue;
        if (properties.containsKey(key)){
            value = Boolean.valueOf(properties.getProperty(key));
        }
        return value;
    }
}
